package org.ajax.test;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import org.ajax.entity.Student;
import org.ajax.entity.User;

import java.util.*;

/**
 * @author XCH
 * @date 2024-11-19
 */
public class GsonTest02 {
    private static void testToJson01() {
        //第一:实例化Java对象
        Student zs = new Student("张三", 20, 99.5,new Date());
        Gson gson = new Gson();
        String json = gson.toJson(zs);
        System.out.println(json);
    }
    private static void testToJson02() {
        //第一:实例化Java对象
        Student zs = new Student("张三", 20, 99.5,new Date());
        //第二:实例化Gson对象
        //setPrettyPrinting()  格式化
        Gson gson = new GsonBuilder().setPrettyPrinting().setDateFormat("yyyy-MM-dd hh:mm:ss:SSS").create();
        //第三:把Java对象转换为JSON字符串
        String json = gson.toJson(zs);
        //第四:输出
        System.out.println(json);
    }
    public static void main(String[] args) {
        testToJson01();
        testToJson02();
    }
}
